package com.qhad.ads.activity;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.songheng.eastnews.apk.R;
import com.juwang.girl.util.QihuAdvertis;
import com.qhad.ads.sdk.adcore.Qhad;
import com.qhad.ads.sdk.interfaces.IQhNativeAd;
import com.qhad.ads.sdk.interfaces.IQhNativeAdListener;
import com.qhad.ads.sdk.interfaces.IQhNativeAdLoader;
import com.qh.feeds.FeedAdapter;
import com.qh.feeds.FeedItem;
import com.qh.feeds.FeedsLoader;
import com.qh.feeds.FeedsLoaderCallback;
import com.qh.feeds.MFeedItem;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class NativeActivity extends Activity {

    private FeedAdapter adapter;
    private ArrayList<FeedItem> feedItems;
    private Button lmButton;
    private View foot;
    private ListView listView;
    private IQhNativeAdLoader nativeLoader;
    private ArrayList<IQhNativeAd> mvNativeAds = new ArrayList<IQhNativeAd>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        foot = getLayoutInflater().inflate(R.layout.listfoot, null);
        lmButton = (Button) foot.findViewById(R.id.ldm);
        lmButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listFeeds();
            }
        });

        listView = (ListView) findViewById(R.id.lst);
        adapter = new FeedAdapter(this.getApplicationContext(), this);
        listView.addFooterView(foot);
        listView.setAdapter(adapter);

        feedItems = new ArrayList<FeedItem>();

        final String adSpaceid = "5uuQaBHBHY";
//        final String adSpaceid = QihuAdvertis.nativeid;
        if (Build.VERSION.SDK_INT > 8) {
            nativeLoader = Qhad.initNativeAdLoader(this, adSpaceid, new IQhNativeAdListener() {

                @Override
                public void onNativeAdLoadSucceeded(ArrayList<IQhNativeAd> nativeAds) {
                    // TODO Auto-generated method stub
                    mvNativeAds.addAll(nativeAds);
                }

                @Override
                public void onNativeAdLoadFailed() {
                    // TODO Auto-generated method stub

                }
            }, false);


            loadFeeds();
        }
    }

    private void listFeeds() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                //nativeLoader.clearAdAttributes();
                //nativeLoader.clearKeywords();

                if (mvNativeAds.size() < 1) {
                    nativeLoader.loadAds();
                }


                int size = feedItems.size();
                for (int i = 0; i < size; i++) {
                    if (i < 5) {
                        FeedItem item = feedItems.remove(0);
                        adapter.addItem(item);

                        if (i % 3 == 0 && mvNativeAds.size() > 0) {
                            IQhNativeAd nativeAd = mvNativeAds.remove(0);
                            if (nativeAd != null) {
                                JSONObject adjson = nativeAd.getContent();

                                try {
                                    FeedItem fitem = new FeedItem();
                                    fitem.title = adjson.getString("title");
                                    fitem.desc = adjson.getString("desc");
                                    fitem.logo = adjson.getString("logo");
                                    fitem.adimg = adjson
                                            .getString("contentimg");
                                    fitem.btntext = adjson.getString("btntext");
                                    fitem.ad = nativeAd;
                                    fitem.isAd = true;
                                    adapter.addItem(fitem);

                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
                size = feedItems.size();
                if (size <= 0) {
//					lmButton.setVisibility(View.GONE);
                    listView.removeFooterView(foot);
                }
            }
        });

    }

    private void loadFeeds() {
        FeedsLoader.getInstance().load(new FeedsLoaderCallback() {

            @Override
            public void onLoaded(final ArrayList<FeedItem> items) {
                feedItems.addAll(0, items);
                listFeeds();
            }

            @Override
            public void whenLoaded(ArrayList<MFeedItem> mitems) {

            }

        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
